package com.zhouchen.shangcheng.activity;

import java.util.ArrayList;
import java.util.List;

import com.zhouchen.shangcheng.R;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends BaseActivity {
//	private LinearLayout mItemShouye,mItemFenLei,mItemGeRenZhX, mItemGouWuChe;
	private int mCurFocusItemId = R.id.item_shouye;
	private ViewPager mViewPager;
	private ViewPagerAdapter mViewPagerAdapter;
	private List<View> mPagerList = new ArrayList<View>();
	private ShouYeView mShouYeView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initViewPager();
		initBottomComp();
		setItemState();
	}
	
	@Override
		protected void onDestroy() {
			// TODO Auto-generated method stub
		mShouYeView.destory();
			super.onDestroy();
		}

	private void initViewPager(){
		mViewPager = (ViewPager)this.findViewById(R.id.viewpager);
		mShouYeView = new ShouYeView(this);
//		mShouYeView.createView();
		mPagerList.add(mShouYeView.createView());
		mPagerList.add(getLayoutInflater().inflate(R.layout.main_fenlei_view, null));
		
		mViewPagerAdapter = new ViewPagerAdapter(mPagerList);
		mViewPager.setAdapter(mViewPagerAdapter);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener(){

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		mViewPager.setCurrentItem(0);
	}
	
	private void initBottomComp(){
		LinearLayout layout = (LinearLayout)this.findViewById(R.id.layoutbottom);
		LinearLayout layoutitem = (LinearLayout)layout.findViewById(R.id.item_shouye);
		layoutitem.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.e("ljl", "--------zhuye-------id = "+v.getId());
				mCurFocusItemId = R.id.item_shouye;
				setItemState();	
				mViewPager.setCurrentItem(0);
			}			
		});
		layoutitem = (LinearLayout)layout.findViewById(R.id.item_fenlei);
		layoutitem.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.e("ljl", "--------fenlei-------id = "+v.getId());
				mCurFocusItemId = R.id.item_fenlei;
				setItemState();	
				mViewPager.setCurrentItem(1);
			}			
		});
		layoutitem = (LinearLayout)layout.findViewById(R.id.item_gerenzhongxing);
		layoutitem.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.e("ljl", "--------geren-------id = "+v.getId());
				mCurFocusItemId = R.id.item_gerenzhongxing;
				setItemState();	
			}			
		});
		layoutitem = (LinearLayout)layout.findViewById(R.id.item_gouwuche);
		layoutitem.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.e("ljl", "--------gouwu-------id = "+v.getId());
				Log.e("ljl", "--------gouwu-------id = "+getResources().getString(R.color.yellow));
				//mCurFocusItemId = R.id.item_gouwuche;
				//setItemState();	
			}			
		});
	}
	
	private void setItemRes(int id, int imgId, int strId, int textcolor){
		LinearLayout layout = (LinearLayout)this.findViewById(R.id.layoutbottom);
		LinearLayout layoutitem = (LinearLayout)layout.findViewById(id);
		ImageView imgview = (ImageView)layoutitem.findViewById(R.id.item_image);
		TextView textview = (TextView)layoutitem.findViewById(R.id.item_text);
		
		if(imgId > 0)
			imgview.setImageResource(imgId);
		if(strId > 0)
			textview.setText(strId);
		String strCore = getResources().getString(textcolor);
		if(strCore != null && !strCore.equals(""))
			textview.setTextColor(Color.parseColor(strCore));
	}
	
	private void setItemState(){
		setItemRes(R.id.item_shouye, R.drawable.img_normal, R.string.shouye, R.color.black);
		setItemRes(R.id.item_fenlei, R.drawable.img_normal, R.string.fenlei, R.color.black);
		setItemRes(R.id.item_gerenzhongxing, R.drawable.img_normal, R.string.gerenzhongxin, R.color.black);
		setItemRes(R.id.item_gouwuche, R.drawable.img_normal, R.string.gouwuche, R.color.black);
		
		setItemRes(mCurFocusItemId, R.drawable.img_focus, -1, R.color.yellow);
	}

	private class ViewPagerAdapter extends PagerAdapter{
		private List<View> mList;
		
		public ViewPagerAdapter(List<View> list){
			mList = list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if(mList == null)
				return 0;
			return mList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}
		
		@Override
		public void destroyItem(View container, int position, Object object) {
			// TODO Auto-generated method stub
			//super.destroyItem(container, position, object);
			if(mList == null)
				return;
			((ViewPager)container).removeView(mList.get(position));
		}
		
		@Override
		public Object instantiateItem(View container, int position) {
			// TODO Auto-generated method stub
			//return super.instantiateItem(container, position);
			if(mList == null)
				return null;
			((ViewPager)container).addView(mList.get(position), 0);
			return mList.get(position);
		}
	}
}
